######################################################################
# Time-stamp: <05/08/11 16:40:04 ostolop>
#
# Routines to validate Data Selection queries
######################################################################
package EP::EPC::Interface::Validator::Data_Selection;
use strict;

use EP::Common;
use EP::Common::General;
use EP::Common::Error qw ( :try );

sub new {
  my $proto = shift;
  my $class = ref($proto) || $proto;
  my $self  = {};
  bless $self, $class;
  return $self;
}

sub validate {
  my $self = shift;
  my ($component, $source) = @_;

  my $err_code = "";

  try {
    checkStdevsNonnegative ( $component, $source );
  } catch EP::Common::Error with {
    $err_code = shift;
  };

  return $err_code;
}

# sub: checkStdevsNonnegative
#   Checks that for standard deviation subselections the number of stdevs specified is non-negative
sub checkStdevsNonnegative {
  my ( $component, $source ) = @_;
  my $stdevs = $component->findvalue ( 'section[@id=2]/subsection[@id=0]/submit[@name=\'subselect_stdev___c_2_s_2_su_0\']' );

  my $numeric_stdevs = 1;
  if ( $stdevs == 0 && $stdevs ne "0" ) {
    $numeric_stdevs = 0;
  }

  if ( not defined $stdevs or
       not ( $numeric_stdevs ) or
       $stdevs < 0
     ) {
    throw EP::Common::Error ( -value => DS_STDEVS_PARAM_INVALID, -err_args => [$stdevs] );
  }

  return 1;
}

1;

